package de.acando.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame implements ActionListener {

	private JPanel panelSteuer = new JPanel();
	private JPanel panelButton = new JPanel();
	
	private JLabel labelEmail = new JLabel("E-Mail:");
	private JLabel labelPasswort = new JLabel("Passwort:");
	
	private JTextField textfieldEmail = new JTextField(20);
	
	private JPasswordField passwortField = new JPasswordField(20);
	
	private JButton buttonLogin = new JButton("Login");
	private JButton buttonAbbrechen = new JButton("Abbrechen");
	private JButton buttonRegistrieren = new JButton("Registrieren");
	
	private String stringEmail = "";
	private String stringPasswort;
	
	public Login(){
		bestandteileAnordnen();
		ereignissensorenAnmeldung();
		erstelleFenster();
	}
	
	private void erstelleFenster(){
		setTitle("Login");
		pack();
		setLocation(430, 250);
		setVisible(true);
		setResizable(false);
	}
	
	private void bestandteileAnordnen(){
		GroupLayout loginLayout = new GroupLayout(getContentPane());
		getContentPane().setLayout(loginLayout);
		
		loginLayout.setAutoCreateGaps(true);
		loginLayout.setAutoCreateContainerGaps(true);
		
		loginLayout.setVerticalGroup(loginLayout.createSequentialGroup().
				addComponent(panelSteuer).
				addComponent(panelButton));
		
		loginLayout.setHorizontalGroup(loginLayout.createParallelGroup().
				addComponent(panelSteuer).
				addComponent(panelButton));
		
		panelSteuerAnordnen();
		panelButtonAnordnen();
	}
	
	private void panelSteuerAnordnen(){
		GroupLayout panelSteuerLayout = new GroupLayout(panelSteuer);
		panelSteuer.setLayout(panelSteuerLayout);
		
		panelSteuerLayout.setAutoCreateGaps(true);
		panelSteuerLayout.setAutoCreateContainerGaps(true);
		
		panelSteuerLayout.setVerticalGroup(panelSteuerLayout.createSequentialGroup().
				addGroup(panelSteuerLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).
						addComponent(labelEmail).
						addComponent(textfieldEmail)).
				addGroup(panelSteuerLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).
						addComponent(labelPasswort).
						addComponent(passwortField)));
		
		panelSteuerLayout.setHorizontalGroup(panelSteuerLayout.createParallelGroup().addGroup(panelSteuerLayout.createSequentialGroup().
				addGroup(panelSteuerLayout.createParallelGroup(GroupLayout.Alignment.LEADING, true).
						addComponent(labelEmail, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).
						addComponent(labelPasswort, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).
				addGroup(panelSteuerLayout.createParallelGroup(GroupLayout.Alignment.LEADING, true).
						addComponent(textfieldEmail, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).
						addComponent(passwortField, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))));
	}
	
	private void panelButtonAnordnen(){
		panelButton.setLayout(new FlowLayout());
		panelButton.add(buttonLogin);
		panelButton.add(buttonAbbrechen);
		panelButton.add(buttonRegistrieren);
	}
	
	private void ereignissensorenAnmeldung(){
		buttonLogin.addActionListener(this);
		buttonAbbrechen.addActionListener(this);
		buttonRegistrieren.addActionListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		
		if(obj == buttonLogin){
			getWerte();
			new MongoDB(stringEmail, stringPasswort);
			//dispose();
		}
		
		if(obj == buttonAbbrechen){
			 dispose();
		}
		
		if(obj == buttonRegistrieren){
			dispose();
			new Registrierung();
		}
	}
	
	private void getWerte(){
		this.stringEmail = textfieldEmail.getText();
		this.stringPasswort = passwortField.getText();
	}
	
	public static void main(String[] args) {
		new Login();
	}
	

}
